package timeline;

import java.io.*;
import java.util.*;
import java.text.SimpleDateFormat;
import java.util.regex.*;

public class PerodicalCollector {

	Util util = null;
	private static final  String indexfile = "../output/webpage/index.result";
	private static final int MAXDAY = 5;
	PerodicalCollector(){
    try{
    	util = Util.getInstance(false);
    	}
        catch(Exception exception) {exception.printStackTrace();}
        
    }

	private static void close(Closeable c) {
	    if (c != null) {
	      try {
	        c.close();
	      } catch (IOException e) {
	        // ignored
	      }
	    }
	}

	boolean checkCpu(int level) throws Exception{
		
		
		String cpuStr = "./checkcpu.sh";
		boolean greater;
		
		Process proc = null;
	    try {
	      ProcessBuilder pb = new ProcessBuilder(cpuStr);
	      proc = pb.start();
	      BufferedReader stdInput = new BufferedReader(new 
	                InputStreamReader(proc.getInputStream()));
	       // read the output from the command
	      String line = stdInput.readLine(); 
	      //System.out.println(line);
	      
	        String cpuus_str = line.substring(line.indexOf(":")+1, line.indexOf("%us"));
	        double cpuus = Double.parseDouble(cpuus_str.trim());
	        		
	        String cpusy_str = line.substring(line.indexOf(",")+1, line.indexOf("%sy"));
	        double cpusy = Double.parseDouble(cpusy_str.trim());
	        
	        //System.out.println("cupus: "+cpuus+" cpusy: "+cpusy);
	        double cputotal = cpuus+cpusy;
	        
	        if (cputotal > level ) 
	        	{
	        	System.out.println("CPU is busy. cpuus: "+cpuus+" cpusy: "+cpusy);
	        	greater = true;
	        	}
	        else{
	        	greater = false;
	        }
	      
	      proc.waitFor();
	    } finally {
	      if (proc != null) {
	        close(proc.getOutputStream());
	        close(proc.getInputStream());
	        close(proc.getErrorStream());
	        proc.destroy();
	      }
	    }
	    return greater;
	}
	/*
	 boolean checkCpu(int level) throws Exception{
		
		String cpuStr = "./checkcpu.sh";
		Runtime.getRuntime().exec(cpuStr);
		Thread.sleep(10000);
		
		FileReader fr = new FileReader("/tmp/mycpu.txt");
        BufferedReader br = new BufferedReader(fr);
        String line;
        line = br.readLine();
        br.close(); fr.close();
        if (line == null) return false; 
        
        String cpuus_str = line.substring(line.indexOf(": ")+2, line.indexOf("%us"));
        double cpuus = Double.parseDouble(cpuus_str.trim());
        		
        String cpusy_str = line.substring(line.indexOf(",")+2, line.indexOf("%sy"));
        double cpusy = Double.parseDouble(cpusy_str.trim());
        
        //System.out.println("cupus: "+cpuus+" cpusy: "+cpusy);
        double cputotal = cpuus+cpusy;
        
        if (cputotal > level ) return true;
        else{
        	System.out.println("CPU is busy. cpuus: "+cpuus+" cpusy: "+cpusy);
        	return false;
        }
	}
	 */
    public static void main(String[] args) {
    	
    	Calendar cal = Calendar.getInstance();
    	PerodicalCollector col = new PerodicalCollector();
    	//start program at xx:00 
        int currentmin = cal.get(Calendar.MINUTE);
		   
		   int sleepto00;
		   if (currentmin==0) sleepto00=0; 
		   else  sleepto00=60-currentmin;
		   
		   System.out.println("Current min is "+currentmin+",  sleeping "+sleepto00+" mins");
		   try{ Thread.sleep(sleepto00*60*1000);}
	       
	        catch(Exception exception) {exception.printStackTrace(); }
	       //System.out.println("Read to processing data");
    	
	   while(true)
	   {
		   //read index file
		   BufferedReader br = null;
		   FileReader fr = null;
		   try{
		   fr = new FileReader(indexfile);
		   br = new BufferedReader(fr);
		   }
		   catch( Exception e ) { e.printStackTrace();continue;}
		   String line = null;
		   Pattern p = Pattern.compile("[0-9]+.*");
		   
		   System.out.println("Scan URL List.........");
		   
		   int lastcurrenthour = -1;
	       while(true)
	       {
	    	   try{
	    	   line = br.readLine();
	    	   }
	    	   catch( Exception e ) { System.out.println("Read Line error");e.printStackTrace();continue;}
	    	   //System.out.println("Line: "+line);
	    	   if (line == null) break;
	           if (line.length()==0) continue;
	           Matcher m = p.matcher(line);
			   if(! m.matches())
			   {
				  // System.out.println("Not vaild line");
				   continue;
			   }
			   
	           String createTimeStr = line.substring(21, 51);
	           //System.out.println("*createTime:  "+createTimeStr);
	           SimpleDateFormat createAtDateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy",Locale.US);
	           Date  createTime = null;
	           try{
	            createTime = createAtDateFormat.parse(createTimeStr);
	           }
    	        catch( Exception e ) { System.out.println("Parse createTime error");e.printStackTrace(); continue;}
	           cal = Calendar.getInstance();
	           int currenthour = cal.get(Calendar.HOUR_OF_DAY);
	           int currentday = cal.get(Calendar.DAY_OF_YEAR );
	           
	           //if this hour does not finish, re-scan the index file for next hour
	           if (lastcurrenthour <0) lastcurrenthour = currenthour;
	           else if (lastcurrenthour != currenthour) break;
	           
	           cal.setTime(createTime);
	           int creathour = cal.get(Calendar.HOUR_OF_DAY);
	           int creatday = cal.get(Calendar.DAY_OF_YEAR );
	           //System.out.println("creat hour: "+creathour+" day: "+creatday+" current hour: "+currenthour+" day:  "+currentday);
	           
	           int days = currentday-creatday;
	           int hours = currenthour-creathour;
	           //System.out.println("Days gap:  "+days+" hours gap: "+hours);
	           if((hours== 0)&&(days >0 )&&(days <MAXDAY))
	           //if((days >0 )&&(days <MAXDAY))
	           {
	        	// wget
	        	   
	        	   //System.out.println("ready to re-wget!");
	        	   ArrayList<String> urlList = col.util.getUrl(line);
	            	Iterator<String> iter = urlList.iterator();
	                 
	                while (iter.hasNext())
	                {
	                	String url = iter.next();
	                	String info = line.substring(line.length()-10, line.length());
	                	//System.out.println("URL: "+url+"Info:"+info);
	                	try{
	                		while (col.checkCpu(95))
		                	{
		                		System.out.println("CPU is busy. wait 15 sec...");
		                		Thread.sleep(10*1000);
		                	}
	                	}
	                	catch( Exception e ) { System.out.println("Get cpu level error"); e.printStackTrace(); continue;}
	                	Runnable runnable = new WgetRunnable(false,createTime, info, url,days,1);
	                	Thread thread = new Thread(runnable);
	                	thread.start();
	                	//System.out.println ("main thread");
	                	
	                 }

	           }
	           else 
	           {
	        	   //System.out.println("Now is not the collection time for this URL.");
	           }
	         }
	   
	       try{
	       br.close();
	       fr.close();
	       }
	       catch( Exception e ) { e.printStackTrace();}
	        //wait next time at xx:00 
	        currentmin = cal.get(Calendar.MINUTE);
			   
			if (currentmin==0) sleepto00=0; 
			   else  sleepto00=60-currentmin;
			   
			   System.out.println("Current min is "+currentmin+",  sleeping "+sleepto00+" mins");
			   try{ Thread.sleep(sleepto00*60*1000);}
		        catch(Exception exception) {exception.printStackTrace();}
		     
	   }
    }	
}
